package com.example.xmpic.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.xmpic.R;
import com.example.xmpic.R2;
import com.example.xmpic.ui.adapter.DialogListAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-03-21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：底部弹出的选择列表
 */
public class BottomListDialog extends Dialog {

    @BindView(R2.id.rv_dialog_list)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_dialog_cancel)
    TextView tvDialogCancel;

    private Context mContext;
    private String[] mMsg;
    private onCallBack mCallBack;

    public BottomListDialog(Context context, onCallBack callBack) {
        super(context, R.style.window_background2);
        this.mContext = context;
        this.mCallBack = callBack;
    }

    public void setMsg(String[] mMsg) {
        this.mMsg = mMsg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_base_list);
        setCanceledOnTouchOutside(false);
        ButterKnife.bind(this);
        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        //dialog显示位置，不设置默认居中
        window.setGravity(Gravity.BOTTOM);
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);

        DialogListAdapter mAdapter = new DialogListAdapter(mContext, mMsg);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setCallBack(i -> {
            dismiss();
            mCallBack.callBack(i);
        });

        tvDialogCancel.setOnClickListener(v -> dismiss());
    }

    public interface onCallBack{
        void callBack(int i);
    }
}
